package com.jianglin.mapper;

import com.jianglin.model.PropertyInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
@Mapper
@Component("propertyInfoDao")
public interface PropertyInfoMapper {

    @Delete("DELETE FROM property_info WHERE house_id=#{houId} AND end_date=#{endDate}")
    void deleteByHouIdAndTime(@Param("houId") Integer houId, @Param("endDate") String onTime);

    @Select("<script>" +
            "SELECT info.*, own.username, hou.numbers, type.name " +
            "FROM property_info info " +
            "LEFT JOIN house hou ON hou.id = info.house_id " +
            "LEFT JOIN `owner` own ON own.house_id = hou.id " +
            "LEFT JOIN property_type type ON type.id = info.type_id " +
            "<where>" +
            "   <if test='house != null and house.numbers != null'>" +
            "       AND hou.numbers LIKE CONCAT('%', #{house.numbers}, '%')" +
            "   </if>" +
            "   <if test='status != null'>" +
            "       AND info.status = #{status}" +
            "   </if>" +
            "   <if test='houseId != null'>" +
            "       AND info.house_id = #{houseId}" +
            "   </if>" +
            "</where>" +
            "</script>")
    @Results({
            @Result(property = "id", column = "id", id = true),
            @Result(property = "typeId", column = "type_id"),
            @Result(property = "money", column = "money"),
            @Result(property = "startDate", column = "start_date"),
            @Result(property = "endDate", column = "end_date"),
            @Result(property = "status", column = "status"),
            @Result(property = "houseId", column = "house_id"),
            @Result(property = "remarks", column = "remarks"),
            @Result(property = "owner.username", column = "username"),
            @Result(property = "house.numbers", column = "numbers"),
            @Result(property = "type.name", column = "name")
    })
    List<PropertyInfo> queryListAll(PropertyInfo propertyInfo);

    @Insert("INSERT INTO property_info (type_id, money, start_date, end_date, status, house_id, remarks) " +
            "VALUES (#{typeId}, #{money}, #{startDate}, #{endDate}, #{status}, #{houseId}, #{remarks})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insert(PropertyInfo propertyInfo);

    @Delete("DELETE FROM property_info WHERE id = #{id}")
    int deleteById(Long id);
    @Update("UPDATE property_info SET status = #{status} WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") Integer status);

    @Select("SELECT * FROM property_info WHERE id = #{id}")
    @Results({
            @Result(property = "id", column = "id", id = true),
            @Result(property = "typeId", column = "type_id"),
            @Result(property = "money", column = "money"),
            @Result(property = "startDate", column = "start_date"),
            @Result(property = "endDate", column = "end_date"),
            @Result(property = "status", column = "status"),
            @Result(property = "houseId", column = "house_id"),
            @Result(property = "remarks", column = "remarks")
    })
    PropertyInfo selectById(Long id);
}
